package edu.sjb_beta1.thread;

import edu.sjb_beta1.characters.Mario;
import edu.sjb_beta1.gui.GUI;
import edu.sjb_beta1.map.EmptySpace;
import edu.sjb_beta1.map.GoalPoint;
import edu.sjb_beta1.map.LogicMapElement;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class ColisionThread extends Thread {
    @Override
    public void run() {
        while (GUI.mario.isAlive()) {
            double marioX = GUI.mario.getPositionX();
            double marioY = GUI.mario.getPositionY();
            int X = (int) marioX / 32;
            int XR = (int) (marioX + 31) / 32;
            int Y = (int) marioY / 32;
            LogicMapElement belowMario =
                    GUI.levelMap.getElementAt(X, Y + 2).getLogicElement();
            LogicMapElement belowRightMario = GUI.levelMap.getElementAt(XR, Y
                    + 2).getLogicElement();
            LogicMapElement rightMario =
                    GUI.levelMap.getElementAt(X + 1, Y + 1).getLogicElement();

            if (GUI.mario.getState() != Mario.JUMP) {
                if (belowMario.getName().equals(EmptySpace.ELEMENT_NAME)
                        && belowRightMario.getName().equals(
                        EmptySpace.ELEMENT_NAME)) {
                    try {
                        GUI.mario.setLocation((int) marioX,
                                (int) marioY + 32);
                        sleep(25);
                    }
                    catch (InterruptedException e) {
                    }
                }
            }

            if (belowMario.getName().equals("Bottomless pit")) {
                GUI.mario.goMuerto();
                GUI.mario.setAlive(false);

                JOptionPane.showMessageDialog(null,
                        "Ud ha perdido!!\n GAME OVER!", "GAME OVER",
                        JOptionPane.INFORMATION_MESSAGE);
                System.exit(255);
            }

            if (rightMario.getName().equals(GoalPoint.ELEMENT_NAME)) {
                GUI.mario.goSalto();
                JOptionPane.showMessageDialog(null,
                        "Felicitaciones! Lo has logrado!", "Super JAVA Bros",
                        JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
                        "./images/objects/star.gif"));
                System.exit(0);
            }

        }
    }
}
